#include "oled_task.h"

oled_menu_t oled_menu;
int down=0,up=0;
int decttt[12]={0x00};

void oled_task(void const * argument)
{
//	while(hal_init_if==False){
//		osDelay(1);
//	}
	oled_init();
	oled_menu_init();
	
	while(1){
		oled_clear();
		if(vision_basic_config.vision_tool_networkmode==OFFLINE){
			oled_blit_str(0,0,"offline:");
			if(vision_basic_config.device_master_if){
				oled_blit_str(49,0,"M");
			}
			else{
				oled_blit_str(49,0,"S");
			}
		}
		else{
			if(vision_basic_config.device_master_if){
				oled_blit_str(0,0,"Master");
			}
			else{
				oled_blit_str(0,0,"Slave");
			}
		}
		oled_blit_num(100,0,vision_basic_config.device_id,0);
		
		if(vision_basic_config.nrf_init_flag==False){
			vision_basic_config.message_type=NRF_NOT_FOUND;
			vision_basic_config.message_if=True;
		}
		switch (oled_menu.oled_menu_id)
		{
			case MENU_MAIN_INFO:
				if(vision_basic_config.menu_data_update_if==False && w25q128_data.flash_init_flag==1){
					get_file_list_from_flash(oled_menu.oled_menu_id);
				}
				if(vision_basic_config.menu_data_update_if==True){
					if(oled_menu.oled_mouse_pos_on_oled>=MAX_SHOW_LINE_ON_TIME){
						oled_menu.oled_mouse_pos_on_oled=MAX_SHOW_LINE_ON_TIME-1;
					}
					for(int i=oled_menu.oled_up_pos_on_oled;i<(oled_menu.oled_up_pos_on_oled+MAX_SHOW_LINE_ON_TIME>oled_menu.oled_menu_list_num[oled_menu.oled_menu_id]?oled_menu.oled_menu_list_num[oled_menu.oled_menu_id]:oled_menu.oled_up_pos_on_oled+MAX_SHOW_LINE_ON_TIME);i++){
						oled_blit_num(0,9+(i-oled_menu.oled_up_pos_on_oled)*8,i+1,0);
						oled_blit_str(8,9+(i-oled_menu.oled_up_pos_on_oled)*8,":");
						oled_blit_str(16,9+(i-oled_menu.oled_up_pos_on_oled)*8,oled_menu.oled_menu_list[oled_menu.oled_menu_id][i].menu_name_show);
					}
					oled_abstract(0,oled_menu.oled_mouse_pos_on_oled*8+9,6*(3+oled_menu.oled_menu_list[oled_menu.oled_menu_id][oled_menu.oled_mouse_pos_in_list].menu_name_len),oled_menu.oled_mouse_pos_on_oled*8+16);
				}
				break;
			case MENU_ROBOT_CONFIG:
				hanz_blit(0,8,han_prt_df,2);
				hanz_blit(0,24,&han_prt_df[2],2);	
//				oled_abstract(1,9,32,40);
				if(target_robot_data.target_color==COLOR_RED){
					color_to_show[0]=HANZ_HONG;
				}
				else{
					color_to_show[0]=HANZ_LAN;
				}
				hanz_blit(0,40,color_to_show,2);
				oled_draw_line(1,40,32,40,1);
				oled_draw_line(34,8,34,56,1);
				oled_draw_line(36,8,36,56,1);
				
				if(target_robot_data.robot_3_amror_size==AMROR_BIG){
					num_3_hanz[1]=HANZ_DA;
				}
				else{
					num_3_hanz[1]=HANZ_XIAO;
				}
				hanz_char_blit(37,8-1,num_3_hanz_char,1);
				hanz_blit(37+8,8-1,num_3_hanz,1);
				hanz_char_blit(37+8+16,8-1,&num_3_hanz_char[1],1);
				hanz_blit(37+8+16+8,8-1,&num_3_hanz[1],3);
				
				if(target_robot_data.robot_4_amror_size==AMROR_BIG){
					num_4_hanz[1]=HANZ_DA;
				}
				else{
					num_4_hanz[1]=HANZ_XIAO;
				}
				hanz_char_blit(37,24-1,num_4_hanz_char,1);
				hanz_blit(37+8,24-1,num_4_hanz,1);
				hanz_char_blit(37+8+16,24-1,&num_4_hanz_char[1],1);
				hanz_blit(37+8+16+8,24-1,&num_4_hanz[1],3);
				
				if(target_robot_data.robot_5_amror_size==AMROR_BIG){
					num_5_hanz[1]=HANZ_DA;
				}
				else{
					num_5_hanz[1]=HANZ_XIAO;
				}
				hanz_char_blit(37,40-1,num_5_hanz_char,1);
				hanz_blit(37+8,40-1,num_5_hanz,1);
				hanz_char_blit(37+8+16,40-1,&num_5_hanz_char[1],1);
				hanz_blit(37+8+16+8,40-1,&num_5_hanz[1],3);
				
				oled_draw_line(37,23,117,23,1);
				oled_draw_line(37,39,117,39,1);
				oled_draw_line(118,8,118,56,1);
				
				if(oled_menu.button_sure_to_choose==False){
					switch(oled_menu.oled_mouse_pos_on_oled){
						case 0:
							oled_abstract(1,42,31,55);
							break;
						case 1:
							oled_abstract(70,9,117,22);
							break;
						case 2:
							oled_abstract(70,9+16,117,22+16);
							break;
						case 3:
							oled_abstract(70,9+32,117,22+32);
							break;
						default:
							oled_menu.oled_mouse_pos_on_oled=0;
							break;
					}
				}
				else{
					if(oled_menu.show_state==True){
						switch(oled_menu.oled_mouse_pos_on_oled){
							case 0:
								oled_abstract(1,42,31,55);
								break;
							case 1:
								oled_abstract(70,9,117,22);
								break;
							case 2:
								oled_abstract(70,9+16,117,22+16);
								break;
							case 3:
								oled_abstract(70,9+32,117,22+32);
								break;
							default:
								oled_menu.oled_mouse_pos_on_oled=0;
								break;
						}
					}
				}
				
				if(oled_menu.sure_cnt>=200){
					oled_menu.sure_cnt=0;
					if(oled_menu.show_state==True){
						oled_menu.show_state=False;
					}
					else{
						oled_menu.show_state=True;
					}
				}
				break;
			case MENU_TOTAL_MENU:
				oled_blit_str(0,9,"rotate_if:");
				if(oled_rotate_if==True){
					oled_blit_str(73,9,"True");
					if(oled_menu.oled_mouse_pos_on_oled==0 && oled_menu.show_state==True){
						oled_abstract(73,9,97,16);
					}
				}
				else{
					oled_blit_str(73,9,"False");
					if(oled_menu.oled_mouse_pos_on_oled==0 && oled_menu.show_state==True){
						oled_abstract(73,9,103,16);
					}
				}
				oled_blit_str(0,17,"MS_set:");
				if(vision_basic_config.device_master_if==True){
					oled_blit_str(73,17,"Master");
					if(oled_menu.oled_mouse_pos_on_oled==1 && oled_menu.show_state==True){
						oled_abstract(73,17,109,24);
					}
				}
				else{
					oled_blit_str(73,17,"Slave");
					if(oled_menu.oled_mouse_pos_on_oled==1 && oled_menu.show_state==True){
						oled_abstract(73,17,103,24);
					}
				}
				if(vision_basic_config.device_master_if==True){
					oled_blit_str(0,25,"tar_id_set:");
					if(vision_basic_config.device_id==0){
						oled_blit_str(73,25,"All");
						if(oled_menu.oled_mouse_pos_on_oled==2 && oled_menu.show_state==True){
							oled_abstract(73,25,89,32);
						}
					}
					else{
						oled_blit_num(73,25,vision_basic_config.device_id,0);
						if(oled_menu.oled_mouse_pos_on_oled==2 && oled_menu.show_state==True){
							oled_abstract(73,25,79,32);
						}
					}
				}
				else{
					oled_blit_str(0,25,"self_id_set:");
					oled_blit_num(73,25,vision_basic_config.device_id,0);
					if(oled_menu.oled_mouse_pos_on_oled==2 && oled_menu.show_state==True){
							oled_abstract(73,25,79,32);
						}
				}
				if(oled_menu.sure_cnt>=200){
					oled_menu.sure_cnt=0;
					if(oled_menu.show_state==True){
						oled_menu.show_state=False;
					}
					else{
						oled_menu.show_state=True;
					}
				}
				break;
			default:
				break;
		}
		if(vision_basic_config.message_if==True){
			if(vision_basic_config.message_type==NRF_NOT_FOUND){
				oled_blit_str(0,56,"nrf device not found");
			}
			else if(vision_basic_config.message_type==SAVING){
				oled_blit_str(0,56,"saving config_data");
			}
			vision_basic_config.message_if=False;
		}
		oled_draw_line(0,7,128,7,1);
		oled_draw_line(0,56,128,56,1);
		oled_update();
		osDelay(1);
	}
}

void key_irq_handle()
{
	if(KEY_SETTING==1 && KEY_SETTING_USE==0){
		oled_menu.oled_menu_id=MENU_TOTAL_MENU;
		oled_menu.oled_mouse_pos_on_oled=0;
		oled_menu.button_sure_to_choose=False;
		KEY_SETTING_LOCK;
	}
	switch(oled_menu.oled_menu_id){
		case MENU_MAIN_INFO:
			main_menu_key_irq();
			break;
		case MENU_ROBOT_CONFIG:
			menu_robot_config_key_irq();
			break;
		case MENU_TOTAL_MENU:
			menu_total_menu_key_irq();
			break;
		default:
			break;
	}
	if(KEY_ENTER==1 && KEY_ENTER_USE==0){
		vision_basic_config.message_type=SAVING;
		vision_basic_config.message_if=True;
		open_file(&(oled_menu.file_read.file_operate),"rob_cfg",7,BOOL_CONFIG,Append);
		oled_menu.file_read.file_read_to_show[0]=target_robot_data.target_color;
		oled_menu.file_read.file_read_to_show[1]=target_robot_data.robot_3_amror_size;
		oled_menu.file_read.file_read_to_show[2]=target_robot_data.robot_4_amror_size;
		oled_menu.file_read.file_read_to_show[3]=target_robot_data.robot_5_amror_size;
		oled_menu.file_read.file_read_to_show[4]=oled_rotate_if;
		oled_menu.file_read.file_read_to_show[5]=vision_basic_config.device_id;
		oled_menu.file_read.file_read_to_show[6]=vision_basic_config.device_master_if;
		file_write_data(&oled_menu.file_read.file_operate,oled_menu.file_read.file_read_to_show,0,7);
		file_close(&oled_menu.file_read.file_operate);
		KEY_ENTER_LOCK;
	}
}

void menu_total_menu_key_irq()
{
	if(KEY_ESCAPE==1 && KEY_ESCAPE_USE==0){
		oled_menu.oled_menu_id=MENU_ROBOT_CONFIG;
		oled_menu.oled_mouse_pos_on_oled=0;
		oled_menu.button_sure_to_choose=False;
		KEY_ESCAPE_LOCK;
	}
	if(KEY_DOWN==1 && KEY_DOWN_USE==0){
		if(oled_menu.button_sure_to_choose==False){
			oled_menu.oled_mouse_pos_on_oled++;
			oled_menu.oled_mouse_pos_on_oled%=3;
		}
		else{
			if(oled_menu.oled_mouse_pos_on_oled==0){
				if(oled_rotate_if==False){
					oled_rotate_if=True;
				}
				else{
					oled_rotate_if=False;
				}
			}
			else if(oled_menu.oled_mouse_pos_on_oled==1){
				if(vision_basic_config.device_master_if==False){
					vision_basic_config.device_master_if=True;
				}
				else{
					vision_basic_config.device_master_if=False;
					if(vision_basic_config.device_id==0){
						vision_basic_config.device_id=1;
					}
				}
			}
			else if(oled_menu.oled_mouse_pos_on_oled==2){
				vision_basic_config.device_id++;
				vision_basic_config.device_id%=10;
				if(vision_basic_config.device_master_if==False){
					if(vision_basic_config.device_id==0){
						vision_basic_config.device_id=1;
					}
				}
			}
		}
		KEY_DOWN_LOCK;
	}
	else if(KEY_UP==1 && KEY_UP_USE==0){
		if(oled_menu.button_sure_to_choose==False){
			if(oled_menu.oled_mouse_pos_on_oled==0){
				oled_menu.oled_mouse_pos_on_oled=2;
			}
			else{
				oled_menu.oled_mouse_pos_on_oled--;
			}
		}
		else{
			if(oled_menu.oled_mouse_pos_on_oled==0){
				if(oled_rotate_if==False){
					oled_rotate_if=True;
				}
				else{
					oled_rotate_if=False;
				}
			}
			else if(oled_menu.oled_mouse_pos_on_oled==1){
				if(vision_basic_config.device_master_if==False){
					vision_basic_config.device_master_if=True;
				}
				else{
					vision_basic_config.device_master_if=False;
					if(vision_basic_config.device_id==0){
						vision_basic_config.device_id=1;
					}
				}
			}
			else if(oled_menu.oled_mouse_pos_on_oled==2){
				if(vision_basic_config.device_master_if==True){
					if(vision_basic_config.device_id==0){
						vision_basic_config.device_id=9;
					}
					else{
						vision_basic_config.device_id--;
					}
				}
				else{
					if(vision_basic_config.device_id==1){
						vision_basic_config.device_id=9;
					}
					else{
						vision_basic_config.device_id--;
					}
				}
			}
		}
		KEY_UP_LOCK;
	}
	else if(KEY_MID==1 && KEY_MID_USE==0){
		if(oled_menu.button_sure_to_choose==True){
			oled_menu.button_sure_to_choose=False;
		}
		else{
			oled_menu.button_sure_to_choose=True;
		}
		KEY_MID_LOCK;
	}
}

void menu_robot_config_key_irq()
{
	if(oled_menu.oled_mouse_pos_on_oled==0){
		if(KEY_RIGHT==1 && KEY_RIGHT_USE==0 && oled_menu.button_sure_to_choose==False){
			oled_menu.oled_mouse_pos_on_oled=1;
			KEY_RIGHT_LOCK;
		}
		else if(KEY_UP==1 && KEY_UP_USE==0 && oled_menu.button_sure_to_choose==True){
			if(target_robot_data.target_color==COLOR_RED){
				target_robot_data.target_color=COLOR_BLUE;
			}
			else{
				target_robot_data.target_color=COLOR_RED;
			}
			KEY_UP_LOCK;
		}
		else if(KEY_DOWN==1 && KEY_DOWN_USE==0 && oled_menu.button_sure_to_choose==True){
			if(target_robot_data.target_color==COLOR_RED){
				target_robot_data.target_color=COLOR_BLUE;
			}
			else{
				target_robot_data.target_color=COLOR_RED;
			}
			KEY_DOWN_LOCK;
		}
	}
	else if(oled_menu.oled_mouse_pos_on_oled>=1 && oled_menu.oled_mouse_pos_on_oled<=3){
		if(KEY_LEFT==1 && KEY_LEFT_USE==0 && oled_menu.button_sure_to_choose==False){
			oled_menu.oled_mouse_pos_on_oled=0;
			KEY_LEFT_LOCK;
		}
		else if(KEY_DOWN==1 && KEY_DOWN_USE==0){
			if(oled_menu.button_sure_to_choose==False){
				oled_menu.oled_mouse_pos_on_oled++;
				if(oled_menu.oled_mouse_pos_on_oled>3){
					oled_menu.oled_mouse_pos_on_oled=1;
				}
			}
			else{
				switch(oled_menu.oled_mouse_pos_on_oled){
					case 1:
						if(target_robot_data.robot_3_amror_size==AMROR_SMALL){
							target_robot_data.robot_3_amror_size=AMROR_BIG;
						}
						else{
							target_robot_data.robot_3_amror_size=AMROR_SMALL;
						}
						break;
					case 2:
						if(target_robot_data.robot_4_amror_size==AMROR_SMALL){
							target_robot_data.robot_4_amror_size=AMROR_BIG;
						}
						else{
							target_robot_data.robot_4_amror_size=AMROR_SMALL;
						}
						break;
					case 3:
						if(target_robot_data.robot_5_amror_size==AMROR_SMALL){
							target_robot_data.robot_5_amror_size=AMROR_BIG;
						}
						else{
							target_robot_data.robot_5_amror_size=AMROR_SMALL;
						}
						break;
				}
			}
			KEY_DOWN_LOCK;
		}
		else if(KEY_UP==1 && KEY_UP_USE==0){
			if(oled_menu.button_sure_to_choose==False){
				oled_menu.oled_mouse_pos_on_oled--;
				if(oled_menu.oled_mouse_pos_on_oled<1){
					oled_menu.oled_mouse_pos_on_oled=3;
				}
			}
			else{
				switch(oled_menu.oled_mouse_pos_on_oled){
					case 1:
						if(target_robot_data.robot_3_amror_size==AMROR_SMALL){
							target_robot_data.robot_3_amror_size=AMROR_BIG;
						}
						else{
							target_robot_data.robot_3_amror_size=AMROR_SMALL;
						}
						break;
					case 2:
						if(target_robot_data.robot_4_amror_size==AMROR_SMALL){
							target_robot_data.robot_4_amror_size=AMROR_BIG;
						}
						else{
							target_robot_data.robot_4_amror_size=AMROR_SMALL;
						}
						break;
					case 3:
						if(target_robot_data.robot_5_amror_size==AMROR_SMALL){
							target_robot_data.robot_5_amror_size=AMROR_BIG;
						}
						else{
							target_robot_data.robot_5_amror_size=AMROR_SMALL;
						}
						break;
				}
			}
			KEY_UP_LOCK;
		}
	}
	if(KEY_MID==1 && KEY_MID_USE==0){
		if(oled_menu.button_sure_to_choose==True){
			oled_menu.button_sure_to_choose=False;
		}
		else{
			oled_menu.button_sure_to_choose=True;
		}
		KEY_MID_LOCK;
	}
}

void main_menu_key_irq()
{
	if(KEY_DOWN==1 && KEY_DOWN_USE==0){
		oled_menu.oled_mouse_pos_in_list++;					
		if(oled_menu.oled_mouse_pos_in_list>=oled_menu.oled_menu_list_num[oled_menu.oled_menu_id]){
			oled_menu.oled_mouse_pos_in_list=oled_menu.oled_menu_list_num[oled_menu.oled_menu_id]-1;
		}
		else{
			oled_menu.oled_mouse_pos_on_oled++;
			if(oled_menu.oled_mouse_pos_on_oled>=MAX_SHOW_LINE_ON_TIME){
				oled_menu.oled_up_pos_on_oled++;
				oled_menu.oled_mouse_pos_on_oled--;
			}
		}
		KEY_DOWN_LOCK;
	}
	else if(KEY_UP==1 && KEY_UP_USE==0){
		oled_menu.oled_mouse_pos_in_list--;
		if(oled_menu.oled_mouse_pos_in_list<0){
			oled_menu.oled_mouse_pos_in_list=0;
		}
		else{
			oled_menu.oled_mouse_pos_on_oled--;
			if(oled_menu.oled_mouse_pos_on_oled<0){
				oled_menu.oled_up_pos_on_oled--;
				oled_menu.oled_mouse_pos_on_oled++;
			}
		}
		KEY_UP_LOCK;
	}
}


void oled_menu_init(void)
{
	vision_basic_config.menu_data_update_if=False;
	oled_menu.oled_menu_id=MENU_ROBOT_CONFIG;
	for(int i=0;i<MAX_ID_NUM;i++){
		oled_menu.oled_menu_list_num[i]=0;
		for(int j=0;j<MAX_MENU_NUM;j++){
			for(int k=0;k<12;k++){
				oled_menu.oled_menu_list[i][j].menu_name_show[k]=0x00;
			}
			oled_menu.oled_menu_list[i][j].menu_name_len=0;
			oled_menu.oled_menu_list[i][j].menu_pos=0;
		}
	}
	oled_menu.oled_up_pos_on_oled=0;
	oled_menu.oled_mouse_pos_on_oled=0;
	oled_menu.oled_mouse_pos_in_list=0;
	oled_menu.button_sure_to_choose=False;
	oled_menu.sure_cnt=0;
	oled_menu.show_state=False;
}

void oled_menu_add(uint8_t* menu_name,uint8_t menu_name_len,uint8_t menu_id_belong)
{
	if(menu_id_belong>MAX_ID_NUM){
		return;
	}
	else{
		oled_menu.oled_menu_list[menu_id_belong][oled_menu.oled_menu_list_num[menu_id_belong]].menu_pos=oled_menu.oled_menu_list_num[menu_id_belong];
		memcpy(oled_menu.oled_menu_list[menu_id_belong][oled_menu.oled_menu_list_num[menu_id_belong]].menu_name_show,menu_name,menu_name_len);
		oled_menu.oled_menu_list[menu_id_belong][oled_menu.oled_menu_list_num[menu_id_belong]].menu_name_len=menu_name_len;
		oled_menu.oled_menu_list_num[menu_id_belong]++;
	}
}

void get_file_list_from_flash(uint8_t id_belong)
{
	uint8_t file_nums=get_nums();
	if(file_nums==0xFF){
		return;
	}
	uint8_t file_name[12]={0x00},name_cnt=0,file[12]={0x00};
	for(int i=0;i<file_nums;i++){
		name_cnt=0;
		flash_read(file_name,(12+i*20),12);
		for(int j=0;j<12;j++){
			if(file_name[j]!=0x00){
				file[name_cnt]=file_name[j];
				name_cnt++;
			}
		}
		oled_menu_add(file,name_cnt,id_belong);
	}
	vision_basic_config.menu_data_update_if=True;
}
